package util;

import cli.ConfigManager;

import java.io.File;
import java.io.FileFilter;

/**
 * My Helper class.
 * Used to allocate different analyzing time for different size apk.
 */
public class ApkSizeAnalyzer {
    public long size;
    public String type;
    public long dataflowTime;
    public long pathReconstructionTime;
    public long pathReconstructionTotal;

    public ApkSizeAnalyzer() {
    }

    private final int SIZE_1MB = 1 * 1024 * 1024;

    // 获取 APK 文件的大小
    public static long getAPKSize(String path) {
        File apkFile = new File(path);
        return apkFile.length();
    }

    // 根据 APK 大小确定映射规则
    public void defaultAnalyze(String path) {
        size = getAPKSize(path);
        type = "small apk";

        final int SMALL_DATAFLOW = 600;
        final int MIDDLE_DATAFLOW = 1200;
        final int LARGE_DATAFLOW = 1800;

        final int SMALL_PER_PATH = 60;
        final int MIDDLE_PER_PATH = 75;
        final int LARGE_PER_PATH = 90;

        final int SMALL_TOTAL = 1200;
        final int MIDDLE_TOTAL = 1200;
        final int LARGE_TOTAL = 1200;

        int SIZE_10MB = 10 * 1024 * 1024;
        int SIZE_50MB = 50 * 1024 * 1024;
        if (size < SIZE_10MB) {  // 小于 10MB small
            dataflowTime = SMALL_DATAFLOW;
            pathReconstructionTime = SMALL_PER_PATH;
            pathReconstructionTotal = SMALL_TOTAL;
//            pathReconstructionTime = 90;
//            pathReconstructionTotal = 2400;
        } else if (size > SIZE_50MB) {  // 大于 50MB - large
            type = "large apk";
            dataflowTime = LARGE_DATAFLOW;
            pathReconstructionTime = LARGE_PER_PATH;
            pathReconstructionTotal = LARGE_TOTAL;
//            pathReconstructionTime = 100;
//            size = 50 * 1024 * 1024;
//            pathReconstructionTotal = 3600;
        } else {
            type = "middle apk";
            dataflowTime = MIDDLE_DATAFLOW;
            pathReconstructionTime = MIDDLE_PER_PATH;
            pathReconstructionTotal = MIDDLE_TOTAL;
        }

        System.out.println("APK 大小: " + Math.round((float) size / (SIZE_1MB)) + " MB");
        System.out.println("APK 类型: " + type);
        System.out.println("数据流分析时间: " + dataflowTime + " 秒");
        System.out.println("路径重建时间: " + pathReconstructionTime + " 秒");
        System.out.println("路径重建总时间: " + pathReconstructionTotal + " 秒");
    }

    public void setAnalyzeResource(String apkPath,ConfigManager configManager){
        size = getAPKSize(apkPath);

        if (size < (long) configManager.SIZE1 * SIZE_1MB) {  // 小于 10MB small
            type = "small apk";
            dataflowTime = configManager.SMALL_APK_DATAFLOW;
            pathReconstructionTime = configManager.SMALL_APK_PER_PATH;
            pathReconstructionTotal = configManager.SMALL_APK_TOTAL;
        } else if (size > (long) configManager.SIZE2 * SIZE_1MB) {  // 大于 50MB - large
            type = "large apk";
            dataflowTime = configManager.LARGE_APK_DATAFLOW;
            pathReconstructionTime = configManager.LARGE_APK_PER_PATH;
            pathReconstructionTotal = configManager.LARGE_APK_TOTAL;
        } else {
            type = "middle apk";
            dataflowTime = configManager.MIDDLE_APK_DATAFLOW;
            pathReconstructionTime = configManager.MIDDLE_APK_PER_PATH;
            pathReconstructionTotal = configManager.MIDDLE_APK_TOTAL;
        }

        System.out.println("APK 大小: " + Math.round((float) size / (SIZE_1MB)) + " MB");
        System.out.println("APK 类型: " + type);
        System.out.println("数据流分析时间: " + dataflowTime + " 秒");
        System.out.println("路径重建时间: " + pathReconstructionTime + " 秒");
        System.out.println("路径重建总时间: " + pathReconstructionTotal + " 秒");
    }


    public static void main(String[] args) {
        String path = "/home/fyl/project/code/AndroPrivacyConsistency/data/apk/RQ1";
        File directory = new File(path);

        FileFilter apkFilter = new FileFilter() {
            @Override
            public boolean accept(File file) {
                return file.getName().endsWith(".apk");
            }
        };

        File[] apkFiles = directory.listFiles(apkFilter);

        if (apkFiles != null) {
            for (File apkFile : apkFiles) {
                ApkSizeAnalyzer analyzer = new ApkSizeAnalyzer();
                analyzer.defaultAnalyze(apkFile.getPath());
            }
        } else {
            System.out.println("无法获取目录中的文件列表。");
        }

    }
}